/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package modules.estoque.models.dao;

/**
 *
 * @author Clezio
 */
import java.util.List;
import modules.estoque.models.dto.Log;
import modules.estoque.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
   
 public class LogDAO{  
     
     private SessionFactory sessionFactory;
     
     public LogDAO() throws Exception{
         sessionFactory = HibernateUtil.getSessionFactory();
     }
       
     public void save(Log log) throws Exception{
         Session session = sessionFactory.openSession();
         Transaction tx = session.beginTransaction();
         if(log.getId() > 0){
            session.update(log);
         }else{
            session.save(log);
         }
         session.flush();
         tx.commit();
         session.close();  
     }  
       
     public java.util.List getList(String condicao) throws Exception{  
         Session session = sessionFactory.openSession();
         Query query = session.createQuery(condicao);
         List logs = query.list();
         session.flush();  
         session.close();  
         return logs;
     }  
       
     public Log retrieve(Integer pk) throws Exception{
         Session session = sessionFactory.openSession();
         Log log = (Log)session.load(Log.class, pk);
         Log logDTO = mountDto(log);
         session.flush();
         session.close();  
         return logDTO;
     }  
       
     public void delete(Log Log) throws Exception{
         Session session = sessionFactory.openSession();
         Transaction tx = session.beginTransaction();
         session.delete(Log);
         session.flush();
         tx.commit();
         session.close();  
     }

    private Log mountDto(Log log) {
        Log logDTO = new Log();
        logDTO.setId(log.getId());
        logDTO.setUsuarios(log.getUsuarios());
        logDTO.setNomeModel(log.getNomeModel());
        logDTO.setModelId(log.getModelId());
        logDTO.setAcao(log.getAcao());
        logDTO.setIp(log.getIp());
        return logDTO;
    }
 }